package xin.marcher.module.lifecycle.service.impl;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.lifecycle.domain.dto.ProductBadIssuesResultDTO;
import xin.marcher.module.lifecycle.domain.request.ProductBadIssuesResultRequest;
import xin.marcher.module.lifecycle.repository.ProductExpriRepository;
import xin.marcher.module.lifecycle.service.ProductBadIssuesResultService;

import java.util.Objects;

/**
 * 货品质量Service
 */
@Service
public class ProductBadIssuesResultServiceImpl implements ProductBadIssuesResultService {

    @Autowired
    private ProductExpriRepository productExpriRepository;

    /**
     * 新增货品质量问题记录
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        // 参数检查
        checkInsertParams(request);
        return productExpriRepository.saveProductBadIssuesResult(request);
    }

    /**
     * 修改货品质量问题记录
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        // 参数检查
        checkUpdateParams(request);
        return productExpriRepository.updateProductBadIssuesResult(request);
    }

    /**
     * 查询货品质量问题记录
     *
     * @param request
     * @return
     */
    @Override
    public ProductBadIssuesResultDTO getProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        return productExpriRepository.getProductBadIssuesResult(request);
    }

    /**
     * 分页查询货品质量问题记录
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<ProductBadIssuesResultDTO> queryProductBadIssuesResultByPage(ProductBadIssuesResultRequest request) {
        return productExpriRepository.queryProductBadIssuesResultByPage(request);
    }

    /**
     * 新增操作参数检查
     *
     * @param request
     */
    private void checkInsertParams(ProductBadIssuesResultRequest request) {
        if (Objects.isNull(request) || StringUtils.isEmpty(request.getItemId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 更新操作的参数检查
     *
     * @param request
     */
    private void checkUpdateParams(ProductBadIssuesResultRequest request) {
        if (Objects.isNull(request) || Objects.isNull(request.getId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
